
var user_server = "https://user.hackerstack.top";
var api_server = "https://api.hackerstack.top";
var js_code = ''


function showLoginWindow() {
    document.getElementById('login_win').style.display = 'block';
    document.getElementById('reg_win').style.display = 'none';
}

function showRegWindow() {
    document.getElementById('login_win').style.display = 'none';
    document.getElementById('reg_win').style.display = 'block';
}

function login(user , pwd , run_function , on_err_function) {
    var xhr = new XMLHttpRequest();
    xhr.open("POST" , user_server+"/login" , true);
    xhr.send(user+"\n"+pwd);
    xhr.onload = function() {
        if (xhr.status == 200) {
            run_function();
            return
        } else{
            on_err_function();
        }
    }
}

function reg(user , pwd ,check_code, run_function , on_err_function) {
    var xhr = new XMLHttpRequest();
    xhr.open("POST" , user_server+"/reg" , true);
    xhr.send(user+"\n"+pwd+"\n"+check_code);
    xhr.onload = function() {
        if (xhr.status == 200) {
            run_function();
            return
        } else{
            on_err_function();
        }
    }
}

function user_reg() {
    var username = document.getElementById('reg_username').value;
    var password = document.getElementById('reg_password').value;
    var check_code = document.getElementById('check_code').value;

    function EnterToMain() {
        showAlert("注册成功" , null);
        document.cookie = "{\"user\" : \""+username+"\" , \"pwd\" : \""+password+"\"}";
        document.getElementById('login_win').style.display = 'none';
        document.getElementById('reg_win').style.display = 'none';
        document.getElementById('login').innerText = "用户: " + username;

        document.getElementById('login').onclick = function() {
            window.location.href = "./user.html";
        }
        //window.location.href = ''
    }

    function on_err() {
        showAlert("注册错误，可能原因:<br />1. 网络错误<br />2. 包含非法字符串<br />3. 账户已经存在<br />4. 验证码错误" , null)
    }

    reg(username , password , check_code , EnterToMain , on_err);
}



function user_login() {
    var username = document.getElementById('username').value;
    var password = document.getElementById('password').value;

    function EnterToMain() {
        document.getElementById('login').innerText = "用户: " + username;
        document.cookie = "{\"user\" : \""+username+"\" , \"pwd\" : \""+password+"\"}";
        document.getElementById('login_win').style.display = 'none';
        document.getElementById('reg_win').style.display = 'none';

        document.getElementById('login').onclick = function() {
            window.location.href = "./user.html";
        }
        //window.location.href = ''
    }

    function on_err() {
        showAlert("用户名或者密码不正确" , null)
    }

    login(username , password , EnterToMain , on_err);
}

function scrollToBottom() {
    const scrollableDiv = document.getElementById('log_console');
    scrollableDiv.scrollTop = scrollableDiv.scrollHeight;
  }
  

function run_log_console() {
    var log_console = document.getElementById('log_console');
    
    // 使用split方法按换行符分割字符串
    var code = js_code.split("\n");
    var i = 0;
    console.log(code)
    setInterval(function() {
        //console.log(i +" "+code.length)
        if (i + i >= code.length) {
            //console.log("hjdfkljsflkjasdlkjfsakldjfdslk")
            i = 0;
        }else {
            log_console.innerHTML += "<a style='color: white'>" + code[i] + "</a><br />";
            i++;
        }
        scrollToBottom()
    } , 200)
}

window.onload = function() {
    try {
        var get_code = new XMLHttpRequest();
        get_code.open("GET" , "./js/web_virus_console.js" , true);
        get_code.send();
        get_code.onload=function() {
            js_code = get_code.responseText;
        }

        var json = JSON.parse(document.cookie);
        var user = json.user;
        var pwd = json.pwd;

        function EnterToMain() {
            document.cookie = "{\"user\" : \""+user+"\" , \"pwd\" : \""+pwd+"\"}";
            document.getElementById('login_win').style.display = 'none';
            document.getElementById('reg_win').style.display = 'none';
            document.getElementById('login').innerText = "用户: " + user;

            document.getElementById('login').onclick = function() {
                window.location.href = "./user.html";
            }
            //window.location.href = ''
        }

        login(user , pwd , EnterToMain , null);
        setTimeout(function() {
            run_log_console();
        } , 1500)
    }catch(e) {
        document.cookie = " ";
        document.cookie = "{\"user\" : \"\" , \"pwd\" : \"\"}";
    }
}

function to_web_virus() {
    var page = document.getElementById('page');
    page.src = './page/web_virus.html';
}

function to_cc_attack() {
    var page = document.getElementById('page');
    page.src = './page/cc_attack.html';
}

function to_mail() {
    var page = document.getElementById('page');
    page.src = './page/mail.html';
}